Use explicit MatchData captures instead of regex globals#43
Open
tas50 wants to merge 1 commit into
Open
Conversation
SubscribesMatcher and NotificationsMatcher parsed their `type[name]` signature by calling String#match for its side effect and then reading the `$1`/`$2` global match variables. Capture the MatchData in a local and read match[1]/match[2] instead — clearer, thread-safe, and not dependent on hidden global state. Signed-off-by: Tim Smith <tsmith84@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SubscribesMatcherandNotificationsMatcherparsed theirtype[name]signature by callingString#matchpurely for its side effect and then reading the magic$1/$2global match variables:The
$1/$2globals are implicit, thread-local-but-easy-to-misuse state that's brittle if any other match runs in between. Capturing theMatchDatain a local and readingmatch[1]/match[2]is clearer and self-contained, with identical behavior.Testing
bundle exec rspec spec/unit/matchers/subscribes_matcher_spec.rb spec/unit/matchers/notifications_matcher_spec.rb→7 examples, 0 failures.bundle exec rspec spec/unit/→162 examples, 0 failures.cookstyle --chefstyle -c .rubocop.yml→ no offenses.